package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.acticity.SearchActivity;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.entity.Disease;

import java.util.List;

/**
 * Created by Administrator on 2016/1/20 0020.
 */
public class DiseaseAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> mList;

    public DiseaseAdapter(Context context, List<T> mList) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T t = mList.get(position);
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_layout_search, null);
            vh = new ViewHolder();
            vh.iv = (ImageView) convertView.findViewById(R.id.iv_adapter_search);
            vh.tv_title = (TextView) convertView.findViewById(R.id.tv_adapter_title);
            vh.tv_num = (TextView) convertView.findViewById(R.id.tv_adapter_num);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (t instanceof Disease.DataEntity.ListEntity) {
            Disease.DataEntity.ListEntity entity = (Disease.DataEntity.ListEntity) t;
            vh.tv_title.setText(replaceStr(entity.getTitle()));
            vh.tv_num.setText(entity.getUp() + "个人觉得有用");
            MyApplication.getApp().getImageLoader().displayImage(
                    entity.getThumb(), vh.iv, MyApplication.getApp().getImageOptions());
        }
        return convertView;
    }

    public String replaceStr(String name) {
        name = name.replace("<font color=\"#00a0e9\">", "");
        name = name.replace("</font>", "");
        return name;
    }

    class ViewHolder {
        ImageView iv;
        TextView tv_title, tv_num;
    }
}
